package com.atguigu.srb.base.handler;

import com.atguigu.srb.base.exception.SrbBussinessException;
import com.atguigu.srb.base.result.R;
import com.atguigu.srb.base.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

/*
    全局异常处理器：原理使用了aop来实现
 */
//@ControllerAdvice 处理所有Controller接口的异常，处理后返回的结果进行转发或重定向
@Slf4j
@RestControllerAdvice //处理所有Controller接口的异常 处理后返回的结果转为json响应
public class GlobalExceptionHandler {
    /*
        运行时异常：无需try catch，代码运行期间可能产生该异常
            RuntimeException
        编译时异常：强制try catch的异常

        当Controller接口被访问时
            如果没有异常，异常处理器不生效
            当出现异常时，能够被异常处理器匹配处理，则调用异常处理方法返回结果
     */
    //创建异常处理器
    //最大异常的处理器
    @ExceptionHandler(value = Exception.class)
    public R exception(Exception e){//一般  异常处理方法形参类型和 ExceptionHandler的value值类型一致
        log.error("异常被捕获：{} , {}", ExceptionUtils.getStackTrace(e), e.getClass().getName());
        return R.error();
    }
    //BadSqlGrammarException：异常处理器
    @ExceptionHandler(value = BadSqlGrammarException.class)
    public R exception(BadSqlGrammarException e){
        log.error("异常被捕获：{}", e.getMessage());
        return R.setResult(ResponseEnum.BAD_SQL_GRAMMAR_ERROR);
    }
    //ArithmeticException
    @ExceptionHandler(value = ArithmeticException.class)
    public R exception(ArithmeticException e){
        log.error("异常被捕获：{}", e.getMessage());
        return R.setResult(ResponseEnum.DIVIDE_ZERO_ERROR);
    }
    //以后controller接口 如果异常希望由此异常处理方法处理，可以捕获异常并转为RuntimeException抛出
    //但是RuntimeException不能携带额外的参数，我们可以对他扩展
//    @ExceptionHandler(value = RuntimeException.class)
//    public R exception(RuntimeException e){
    @ExceptionHandler(value = SrbBussinessException.class)
    public R exception(SrbBussinessException e){
        log.error("SrbBussinessException异常被捕获：{}", e.getMessage());
        return R.error().code(e.getCode()).message(e.getMsg());
    }

    /**
     * Controller上一层相关异常
     */
    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            MethodArgumentNotValidException.class,
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public R handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        //SERVLET_ERROR(-102, "servlet请求异常"),
        return R.error().message(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());
    }
}
